/**
 * 二叉树
 */

public class BinaryTree {

    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode creatTree () {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;//根节点
    }

    //前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");//打印根节点
        preOrder(root.left);//遍历左子树
        preOrder(root.right);//遍历右子树
    }

    //中序遍历
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);//遍历左子树
        System.out.print(root.val + " ");//打印根节点
        inOrder(root.right);//遍历右子树
    }

    //后序遍历
    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);//遍历左子树
        postOrder(root.right);//遍历右子树
        System.out.print(root.val + " ");//打印根节点
    }

    public int nodeSize;

    // 获取树中节点的个数 - 方式一
    public int getNodeSize(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return getNodeSize(root.left) + getNodeSize(root.right) + 1;
    }
    // 获取树中节点的个数 - 方式二
    public void getNodeSize2(TreeNode root) {
        if (root == null) {
            return;
        }
        nodeSize++;
        getNodeSize2(root.left);
        getNodeSize2(root.right);
    }



    public int leafCount;

    //方式一  遍历思路
    public void getLeafNodeCount(TreeNode root) {
        if(root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            leafCount++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
    }

    // 获取叶子节点的个数 - 方式二
    // 子问题思路-求叶子结点个数
    public int getLeafNodeCount2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }





    // 获取第K层节点的个数
    // 例如：获取A第三层节点的个数，就是获取左子树B的第二层和右子树C的第二层，也就是获得DEFG的第一层！！！
    public int getKLevelNodeCount(TreeNode root,int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount( root.left,k-1 ) + getKLevelNodeCount(root.right,k-1);
    }



    // 获取二叉树的高度
    public int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight =getHeight(root.right);
        return Math.max(leftHeight,rightHeight) +1;
    }



    // 检测值为value的元素是否存在
    public TreeNode find(TreeNode root, char val){
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode leftNodeVal = find(root.left,val);
        if (leftNodeVal != null) {
            return leftNodeVal;
        }
        TreeNode rightNodeVal = find(root.right,val);
        if (rightNodeVal != null) {
            return rightNodeVal;
        }
        return null;
    }

    public TreeNode lowerCommonAncestor(TreeNode root,TreeNode p,TreeNode q) {
        if (root == null) {
            return root;
        }
        if (root == q || root == p) {
            return root;
        }
        TreeNode leftTree = lowerCommonAncestor(root.left,p,q);
        TreeNode rightTree = lowerCommonAncestor(root.right,p,q);
        if (leftTree != null && rightTree != null) {
            return root;
        }else if (leftTree != null) {
            return leftTree;
        }else {
            return rightTree;
        }
    }

    /*

    //层序遍历
    public void levelOrder(TreeNode root) {

    }
    // 判断一棵树是不是完全二叉树
    public boolean isCompleteTree(TreeNode root){
        return false;
    }

    */

}

/*public class BinaryTree {
    //孩子表示法
    int value;//数据域
    Node left;//左子树
    Node right;//右子树
}

class BinaryTree2 {
    //孩子双亲表示法
    int value;//数据域
    Node left;//左子树
    Node right;//右子树
    Node parent;//表示当前结点的根节点
}*/
